package cn.iocoder.yudao.module.bill.dal.mysql.bill;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.bill.dal.dataobject.bill.BillDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.bill.controller.admin.bill.vo.*;

/**
 * 账单信息 Mapper
 *
 * @author mmciel
 */
@Mapper
public interface BillMapper extends BaseMapperX<BillDO> {

    default PageResult<BillDO> selectPage(BillPageReqVO reqVO) {

        LambdaQueryWrapperX<BillDO> wrapperX = new LambdaQueryWrapperX<BillDO>()
                .likeIfPresent(BillDO::getBillTitle, reqVO.getBillTitle())
                .likeIfPresent(BillDO::getBillRemark, reqVO.getBillRemark())
                .eqIfPresent(BillDO::getBillAmount, reqVO.getBillAmount())
                .eqIfPresent(BillDO::getAmountType, reqVO.getAmountType())
                .eqIfPresent(BillDO::getAccountId, reqVO.getAccountId())
                .eqIfPresent(BillDO::getCate1, reqVO.getCate1())
                .eqIfPresent(BillDO::getCate2, reqVO.getCate2())
                .eqIfPresent(BillDO::getCate3, reqVO.getCate3())
                //.likeIfPresent(BillDO::getTags, reqVO.getTags())
                .eqIfPresent(BillDO::getTemplateId, reqVO.getTemplateId())
                .eqIfPresent(BillDO::getBillType, reqVO.getBillType())
                .eqIfPresent(BillDO::getBillStage, reqVO.getBillStage())
                .eqIfPresent(BillDO::getBillStatus, reqVO.getBillStatus())
                .eqIfPresent(BillDO::getTransAccountId, reqVO.getTransAccountId())
                .betweenIfPresent(BillDO::getBillTime, reqVO.getBillTime())
                .likeIfPresent(BillDO::getExtRemark, reqVO.getExtRemark())
                .eqIfPresent(BillDO::getExtFromAccount, reqVO.getExtFromAccount())
                .eqIfPresent(BillDO::getExtFromOwner, reqVO.getExtFromOwner())
                .eqIfPresent(BillDO::getExtToAccount, reqVO.getExtToAccount())
                .eqIfPresent(BillDO::getExtToOwner, reqVO.getExtToOwner())
                .eqIfPresent(BillDO::getExtInfo, reqVO.getExtInfo())
                .eqIfPresent(BillDO::getExtBillSource, reqVO.getExtBillSource())
                .eqIfPresent(BillDO::getExtParam, reqVO.getExtParam())
                .eqIfPresent(BillDO::getOrderNum, reqVO.getOrderNum())
                .eqIfPresent(BillDO::getHasProperty, reqVO.getHasProperty())
                .likeIfPresent(BillDO::getPropertyName, reqVO.getPropertyName())
                .betweenIfPresent(BillDO::getPropertyCheckTime, reqVO.getPropertyCheckTime())
                .eqIfPresent(BillDO::getPropertyCate, reqVO.getPropertyCate())
                .eqIfPresent(BillDO::getPropertyValue, reqVO.getPropertyValue())
                .likeIfPresent(BillDO::getPropertyParam, reqVO.getPropertyParam())
                .likeIfPresent(BillDO::getPropertyRemark, reqVO.getPropertyRemark())
                .eqIfPresent(BillDO::getPropertyStatus, reqVO.getPropertyStatus())
                .betweenIfPresent(BillDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(BillDO::getId);
        if (StringUtils.isNotBlank(reqVO.getTags())){
            List<String> tags = Arrays.stream(reqVO.getTags().split("@"))
                    .filter(StringUtils::isNotBlank) // 过滤空标签
                    .toList();
            if (!tags.isEmpty()) {
                wrapperX.and(wrapper -> {
                    Iterator<String> iterator = tags.iterator();
                    wrapper.like(BillDO::getTags, iterator.next()); // 第一个条件
                    while (iterator.hasNext()) {
                        wrapper.or().like(BillDO::getTags, iterator.next()); // 后续用 OR 连接
                    }
                });
            }
        }

        return selectPage(reqVO, wrapperX);
    }

}